﻿#include "FITKAbaqusIOINPInterface.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITKAbaqusAdaptorCase.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

namespace IO
{
    void FITKAbaqusINPIO::setReadLevel(INPReadLevel level)
    {
        _readLevel = level;
    }

    void FITKAbaqusINPIO::setFileName(const QString& fileName)
    {
        _fileName = fileName;
    }

    QString FITKAbaqusINPIO::getComponentName()
    {
        //返回类型名
        return "AbaqusINPIO";
    }

    void FITKAbaqusINPIO::setDataObject(Core::FITKAbstractDataObject* dataObject)
    {
        _dataObject = dataObject;
    }
     
    bool FITKAbaqusINPIO::exec(const int indexPort)
    {
        _success = false;
        //if (io != nullptr)  return false;
        if (_dataObject == nullptr) return false;
        if (_fileName.isEmpty()) return false;
         
        //判断indexPort 1：读取 2：写出
        switch (indexPort)
        {
        case 1:
            _io = new FITKAbaqusINPReader(_readLevel);
            break;
        case 2:
            _io = new FITKAbaqusINPWriter;
            break;
        default: break;
        }
        if (_io == nullptr ) return false;
         
        connect(_io.getObjectPtr(), SIGNAL(taskFinishedSig(FITKThreadTask*)), this, SLOT(ioThreadFinishedSlot()),Qt::UniqueConnection);

        _io->setFileName(_fileName);
        _io->setDataObject(_dataObject);
        _io->setResultMark(&_success);
        //在线程运行，提交到线程池
        if (_runInThread)
            _io->push2ThreadPool();
        else
        {
            _io->run();
            //不在线程运行，执行完读写之后直接触发结束信号
            emit ioThreadFinishedSig();
        }
        return true;
    }


    bool FITKAbaqusINPIO::isSuccess()
    {
        return _success;
    }

    bool FITKAbaqusINPIO::isRunning()
    {
        return  _io != nullptr;
    }

    void FITKAbaqusINPIO::setRunInThread(bool r /*= true*/)
    {
        _runInThread = r;
    }

    void FITKAbaqusINPIO::ioThreadFinishedSlot()
    {
//        disconnect(_io.getObjectPtr(), SIGNAL(taskFinishedSig(FITKThreadTask*)), this, SLOT(ioThreadFinishedSlot()));

        emit this->ioThreadFinishedSig();
      //  _io = nullptr;
    }

}



